package com.keon.jianbao.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.keon.jianbao.R;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.base.SimpleAdapter;
import com.keon.jianbao.bean.NetWorkImg;
import com.keon.jianbao.ui.activity.PicDetailActivity;
import com.keon.jianbao.ui.activity.TweetPublishActivity;
import com.keon.jianbao.util.UILUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

/**
 * Created by keon on 2015/9/6.
 */
public class UploadImgAdapter extends SimpleAdapter<NetWorkImg> {

    private int maxImageNum = 6;
    private ArrayList<String> imgurls = new ArrayList();

    public UploadImgAdapter(ArrayList data) {
        initData(data);
    }

    public UploadImgAdapter(ArrayList data, int maxImageNum) {
        this.maxImageNum = maxImageNum;
        initData(data);
    }

    public UploadImgAdapter(int maxImageNum) {
        this.maxImageNum = maxImageNum;

        NetWorkImg netWorkImg = new NetWorkImg(NetWorkImg.TYPE_ADD_BTN, "", NetWorkImg.ACTION_ADD);
        ArrayList data = new ArrayList(1);
        data.add(netWorkImg);
        initData(data);
    }

    static class ViewHolder {
        @Bind(R.id.tweet_img)
        ImageView ivImg;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public void updateData(ArrayList<String> data) {
        imgurls = data;
        int s = data.size();

        ArrayList newData = new ArrayList();
        for (int i = 0; i < (s > maxImageNum ? maxImageNum : s); i++) {
            NetWorkImg netWorkImg = new NetWorkImg(NetWorkImg.TYPE_COMMON, "file://" + data.get(i), NetWorkImg.ACTION_ADD, true);
            newData.add(netWorkImg);
        }

        if (s < maxImageNum) {
            NetWorkImg netWorkImg = new NetWorkImg(NetWorkImg.TYPE_ADD_BTN, "", NetWorkImg.ACTION_ADD);
            newData.add(netWorkImg);
        }

        setData(newData);
    }

    @Override
    protected View getRealView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = getLayoutInflater(parent.getContext()).inflate(
                    R.layout.list_cell_tweet_img, null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        final NetWorkImg netWorkImg = mDatas.get(position);

        if (netWorkImg.getUri() != null) {
//            vh.ivImg.setImageURI(netWorkImg.getUri());
            ImageLoader.getInstance().displayImage(netWorkImg.getUri().toString(), vh.ivImg, UILUtils.getOptions2());
        }

        if (netWorkImg.getAction() == NetWorkImg.ACTION_ADD) {
            vh.ivImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent intent = new Intent(mContext, MultiImageSelectorActivity.class);

// whether show camera
                    intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);

// max select image amount
                    intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, maxImageNum);

// select mode (MultiImageSelectorActivity.MODE_SINGLE OR MultiImageSelectorActivity.MODE_MULTI)
                    intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);

                    if (imgurls != null && imgurls.size() > 0) {
                        intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, imgurls);
                    }

                    BaseActivity ba = (BaseActivity) mContext;
                    ba.startActivityForResult(intent, TweetPublishActivity.REQUEST_PICK_PHOTO);
                }
            });

        } else {
            vh.ivImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseActivity ba = (BaseActivity) mContext;
                    Bundle bundle = new Bundle();
                    ArrayList<String> d = new ArrayList<String>(mDatas.size());
                    for (int i = 0; i < mDatas.size(); i++) {
                        d.add(mDatas.get(i).getUri().toString());
                    }
                    bundle.putStringArrayList("uris", d);
                    bundle.putInt("position", position);
                    ba.jump(PicDetailActivity.class, bundle);
                }
            });
        }

        return convertView;
    }
}
